import 'reflect-metadata'
const REQUIRED_PARAMETERS = 'REQUIRED_PARAMETERS'
function validate(target, propertyKey, parameterIndex) {
  const existingRequiredParameters =
    Reflect.getOwnMetadata(REQUIRED_PARAMETERS, target, propertyKey) || []
  existingRequiredParameters.push(parameterIndex)
  Reflect.defineMetadata(
    REQUIRED_PARAMETERS,
    existingRequiredParameters,
    target,
    propertyKey
  )
  console.log(111)
}
function validateParameters(target, propertyKey, descriptor) {
  const originalMethod = descriptor.value
  descriptor.value = function (...args) {
    const existingRequiredParameters =
      Reflect.getOwnMetadata(REQUIRED_PARAMETERS, target, propertyKey) || []
    console.log('existingRequiredParameters', existingRequiredParameters)
    for (let parameterIndex of existingRequiredParameters) {
      if (args[parameterIndex] === undefined) {
        throw new Error(`必填${parameterIndex}`)
      }
    }
    return originalMethod.apply(this, args)
  }
}
class User {
  constructor(private name, private age) {}
  @validateParameters
  setName(newName, @validate age) {
    this.name = newName
    this.age = age
  }
}
const user = new User('Alice', 10)
// user.setName('Bol')
user.setName(undefined, undefined)
